package Frame.systemAdministratorFrame;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.Point;
import java.awt.Toolkit;
import javax.swing.JButton;
import javax.swing.JFrame;
import java.util.Iterator;
import java.util.List;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellRenderer;

import Frame.common.BackgroundFrame;
import Frame.common.RegisterFrame;
import Frame.common.TableBackGround;
import Frame.librarianFrame.BookManageFrame;
import Frame.librarianFrame.LbBackgroundFrame;
import controller.Controller;
import table.User;
import javax.swing.JTable;
import javax.swing.JScrollPane;
import javax.swing.JRadioButton;
import javax.swing.JComboBox;
import javax.swing.ButtonGroup;
import javax.swing.DefaultComboBoxModel;
import java.awt.event.ActionListener;
import java.net.URL;
import java.awt.event.ActionEvent;
import java.awt.SystemColor;
import javax.swing.ImageIcon;
import java.awt.Color;
import java.awt.Component;

public class sUserManageFrame extends JFrame {
	private JTextField tf1;
	private JTextField tf2;
	private JTable userTable;
	private JTextField tf5;
	private JComboBox<String> comboBox;
	private JComboBox<String> comboBox2;
	private JPanel panel1;
	private JPanel panel2;
	private JTextField tf3;
	private JScrollPane tablePanel;// 用户信息表的panel
	private String radioText;
	private JRadioButton bt4;
	private JRadioButton bt5;
	private JTextField tf7;
	private JTextField tf4;
	private JPanel contentPane1;
	private Controller controller;
	private JButton bt7;
	private ButtonGroup group;
	public sUserManageFrame(Controller controller) {   
		this.controller = controller;
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); // 获取屏幕大小
		this.setSize(screenSize.width, screenSize.height);
		contentPane1 = new JPanel(); 
		contentPane1.setBounds(0, 0, 1920, 930); 
		contentPane1.setLayout(null);			
		contentPane1.add(panel1()); 
		contentPane1.add(panel2());
		contentPane1.add(getUserTablePanel(controller.getSystemAdministratorService().findAll()));
	}

	public JPanel panel1() {
		panel1 = new JPanel();
		panel1.setBackground(Color.WHITE);
		panel1.setLocation(0, 0);
		panel1.setSize(1920, 116);
		panel1.setLayout(null);
		JLabel lb1 = new JLabel("用户名:");
		lb1.setFont(new Font("华文楷体", Font.PLAIN, 20));
		lb1.setBounds(234, 48, 64, 27);
		panel1.add(lb1);

		tf1 = new JTextField();
		tf1.setColumns(10);
		tf1.setBounds(316, 48, 147, 31);
		tf1.setFont(new Font("华文楷体", Font.PLAIN, 20));
		panel1.add(tf1); 
		JLabel lb2 = new JLabel("姓名:");
		lb2.setFont(new Font("华文楷体", Font.PLAIN, 20));
		lb2.setBounds(697, 48, 44, 27);
		panel1.add(lb2);

		tf2 = new JTextField();
		tf2.setBounds(755, 46, 125, 31);
		tf2.setFont(new Font("华文楷体", Font.PLAIN, 20));
		panel1.add(tf2);
		tf2.setColumns(10);

		JButton bt3 = new JButton("查询");
		bt3.setIcon(new ImageIcon(sUserManageFrame.class.getResource("/logo/搜索.png")));
		bt3.setFont(new Font("华文楷体", Font.PLAIN, 20));
		bt3.setBounds(1470, 44, 119, 35);
		panel1.add(bt3);
		bt3.addActionListener(controller);
		bt3.setActionCommand("findUser");
		comboBox = new JComboBox<String>();
		comboBox.setForeground(Color.BLACK);
		comboBox.setFont(new Font("华文楷体", Font.PLAIN, 20));
		comboBox.setModel(new DefaultComboBoxModel(new String[] { "系统管理员", "图书管理员", "读者" }));
		comboBox.setBounds(1111, 45, 138, 33);
		panel1.add(comboBox);
		comboBox.addActionListener(controller);
		comboBox.setActionCommand("identity");
		
		bt7 = new JButton("返回");
		bt7.setIcon(new ImageIcon(sUserManageFrame.class.getResource("/logo/搜索.png")));
		bt7.setFont(new Font("华文楷体", Font.PLAIN, 20));
		bt7.addActionListener(controller);
		bt7.setActionCommand("returnFindAllUser");
		bt7.setBounds(1716, 44, 119, 35);
		panel1.add(bt7);
		return panel1;
	}

	public JPanel panel2() {
		panel2 = new JPanel();
		panel2.setBackground(Color.WHITE);
		panel2.setSize(1920, 329);
		panel2.setLocation(0, 600);
		panel2.setLayout(null);
		JButton bt6 = new JButton("添加");
		bt6.setIcon(new ImageIcon(sUserManageFrame.class.getResource("/logo/添加.png")));
		bt6.setBounds(434, 210, 119, 35);
		bt6.setFont(new Font("华文楷体", Font.PLAIN, 20));
		panel2.add(bt6);
		bt6.addActionListener(controller);
		bt6.setActionCommand("saveUser");

		JButton bt7 = new JButton("修改");
		bt7.setIcon(new ImageIcon(sUserManageFrame.class.getResource("/logo/编辑.png")));
		bt7.setBounds(914, 210, 119, 35);
		bt7.setFont(new Font("华文楷体", Font.PLAIN, 20));
		panel2.add(bt7);
		bt7.addActionListener(controller);
		bt7.setActionCommand("updateUser");

		JButton bt8 = new JButton("删除");
		bt8.setIcon(new ImageIcon(sUserManageFrame.class.getResource("/logo/删除.png")));
		bt8.setBounds(1367, 210, 119, 35);
		bt8.setFont(new Font("华文楷体", Font.PLAIN, 20));
		panel2.add(bt8);
		bt8.addActionListener(controller);
		bt8.setActionCommand("deleteUser");

		JLabel lb3 = new JLabel("用户名:");
		lb3.setBounds(227, 44, 64, 27);
		lb3.setFont(new Font("华文楷体", Font.PLAIN, 20));
		panel2.add(lb3);

		JLabel lb4 = new JLabel("姓名:");
		lb4.setBounds(247, 125, 44, 27);
		lb4.setFont(new Font("华文楷体", Font.PLAIN, 20));
		panel2.add(lb4);

		JLabel lb5 = new JLabel("密码:");
		lb5.setBounds(860, 44, 44, 27);
		lb5.setFont(new Font("华文楷体", Font.PLAIN, 20));
		panel2.add(lb5);

		JLabel lb7 = new JLabel("身份:");
		lb7.setFont(new Font("华文楷体", Font.PLAIN, 20));
		lb7.setBounds(1468, 125, 44, 27);
		panel2.add(lb7);

		JLabel lb8 = new JLabel("电话:");
		lb8.setFont(new Font("华文楷体", Font.PLAIN, 20));
		lb8.setBounds(860, 125, 44, 27);
		panel2.add(lb8);

		tf3 = new JTextField();
		tf3.setBackground(Color.WHITE);
		tf3.setBounds(305, 42, 156, 31);
		tf3.setColumns(10);
		tf3.setFont(new Font("华文楷体", Font.PLAIN, 20));
		panel2.add(tf3);

		tf4 = new JTextField();
		tf4.setFont(new Font("华文楷体", Font.PLAIN, 20));
		tf4.setColumns(10);
		tf4.setBounds(305, 125, 125, 31);
		panel2.add(tf4);

		tf5 = new JTextField();
		tf5.setBackground(Color.WHITE);
		tf5.setBounds(925, 42, 156, 31);
		tf5.setColumns(10);
		tf5.setFont(new Font("华文楷体", Font.PLAIN, 20));
		panel2.add(tf5);

		tf7 = new JTextField();
		tf7.setColumns(10);
		tf7.setBounds(925, 123, 156, 31);
		tf7.setFont(new Font("华文楷体", Font.PLAIN, 20));
		panel2.add(tf7);

		comboBox2 = new JComboBox<String>();
		comboBox2.setForeground(Color.BLACK);
		comboBox2.setBackground(Color.WHITE);
		comboBox2.setModel(new DefaultComboBoxModel(new String[] {"","系统管理员", "图书管理员", "读者" }));
		comboBox2.setFont(new Font("华文楷体", Font.PLAIN, 20));
		comboBox2.setBounds(1542, 122, 138, 33);
		panel2.add(comboBox2);

		group = new ButtonGroup();// 归组才能是实现单选 
		bt4 = new JRadioButton("男");
		bt4.setBackground(Color.WHITE);
		bt4.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (bt4.isSelected()) {
					radioText = bt4.getText(); 
				}
			}
		});
		bt4.setBounds(1537, 40, 49, 35);
		bt4.setFont(new Font("华文楷体", Font.PLAIN, 20));
		group.add(bt4);
		panel2.add(bt4);

		bt5 = new JRadioButton("女");
		bt5.setBackground(Color.WHITE);
		bt5.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (bt5.isSelected()) {
					radioText = bt5.getText();
				}
			}
		});
		bt5.setBounds(1608, 40, 49, 35);
		bt5.setFont(new Font("华文楷体", Font.PLAIN, 20));
		group.add(bt5);
		panel2.add(bt5);
		
		JLabel lb9 = new JLabel("性别:");
		lb9.setFont(new Font("华文楷体", Font.PLAIN, 20));
		lb9.setBounds(1468, 44, 44, 27);
		panel2.add(lb9);
		return panel2;
	}

	/**
	 * 滚动表格
	 * 
	 * @param userList
	 * @return
	 */
	public JScrollPane getUserTablePanel(List<User> userList) {
		Object dataName[] = { "用户名", "姓名", "身份", "密码", "电话", "性别" };
		Object data[][] = new Object[userList.size()][7];
		Iterator<User> iter = userList.iterator();
		int i = 0;
		while (iter.hasNext()) {
			User us = iter.next();
			data[i][0] = us.getLoginName();
			data[i][1] = us.getUserName(); 
			data[i][2] = us.getIdentity();
			data[i][3] = us.getLoginPassword();
			data[i][4] = us.getUserPhoneNumber();
			data[i][5] = us.getUserSex();
			i++;
		}
		userTable = new JTable(data, dataName) {
			@Override
		    public Component prepareRenderer(TableCellRenderer renderer, int row, int column){//设置鼠标光标划过时的颜色改变
	        Component comp = super.prepareRenderer(renderer,row,column); 
		        Point p = getMousePosition();
		    	
		        if(p!=null){
		            int rowUnderMouse = rowAtPoint(p);
		            if(rowUnderMouse == row){
		                  comp.setBackground(new Color(223,240,216));
		           }
//		            else{
//		                  comp.setBackground(DefaultLookup.getColor(this,ui,"Table.alternateRowColor"));
//		           }
		        }		        
		        return comp; 
		    }
			private static final long serialVersionUID = 1L;
			public boolean isCellEditable(int row, int column) {
				return false;
			};// 将表设置成无法修改 
		};
		userTable.setSelectionBackground(new Color(223,240,216));//设置选中的行的颜色
		TableBackGround.setColumnColor(userTable);//设置表的交替行数的颜色 
		userTable.addMouseMotionListener(controller.getMyTableMouseMotion());
		userTable.getTableHeader().setBackground(new Color(184,207,240));//设置表头的颜色
		userTable.getTableHeader().setFont(new Font("华文楷体", Font.PLAIN, 20)); // 表头字体
		userTable.setFont(new Font("华文楷体", Font.PLAIN, 21)); // 表体字体
		userTable.getTableHeader().setPreferredSize(new Dimension(15, 35)); // 表头高度 先后为宽 高
		DefaultTableCellRenderer cr = new DefaultTableCellRenderer();// 将表体的内容设置为居中显示
		cr.setHorizontalAlignment(JLabel.CENTER);
		userTable.setDefaultRenderer(Object.class, cr);
		userTable.setRowHeight(userTable.getRowHeight() + 15); // 表体高度
		userTable.setRowMargin(5);// 设置相邻行中的单元格之间的空白空间量
		userTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); // 表格只能选择一行
		userTable.addMouseListener(controller);
		userTable.setName("getTableMessage");
		userTable.repaint();
		tablePanel = new JScrollPane(userTable);
		tablePanel.getViewport().setBackground(Color.WHITE);//将JScrollPane设置成白色
		tablePanel.setLocation(0, 115);
		tablePanel.setSize(1920, 486);
		return tablePanel;
	}

	public void showSearchResult(List<User> result) { 
		contentPane1.remove(tablePanel);
		contentPane1.add(getUserTablePanel(result));
		contentPane1.validate();
		JOptionPane.showMessageDialog(this, "重新加载数据成功！");
	}

	public void refresh() { 
		contentPane1.remove(tablePanel);
		contentPane1.add(getUserTablePanel(controller.getSystemAdministratorService().findAll()));
		contentPane1.validate();
		JOptionPane.showMessageDialog(this, "重新加载数据成功！");
	}
	//通过性别信息 使得单选框被选中
	public void JudgeJRadioButton(String x) {
		if ("男".equals(x)) { // String类型不能用‘==’来判断是否相等，应该用equals()方法来判断
			bt4.setSelected(true);
			setRadioText(x);
		} else {
			bt5.setSelected(true);
			setRadioText("女");
		}
	} 

	public JTextField getTf1() {
		return tf1;
	}

	public void setTf1(JTextField tf1) {
		this.tf1 = tf1;
	}

	public JTextField getTf2() {
		return tf2;
	}

	public void setTf2(JTextField tf2) {
		this.tf2 = tf2;
	}

	public JTextField getTf3() {
		return tf3;
	}

	public void setTf3(JTextField tf3) {
		this.tf3 = tf3;
	}

	public JTextField getTf4() {
		return tf4;
	}

	public void setTf4(JTextField tf4) {
		this.tf4 = tf4;
	}

	public JTextField getTf5() {
		return tf5;
	}

	public void setTf5(JPasswordField tf5) {
		this.tf5 =  tf5;
	}

	public JComboBox<String> getComboBox() {
		return comboBox;
	}

	public void setComboBox(JComboBox<String> comboBox) {
		this.comboBox = comboBox;
	}

	public JComboBox<String> getComboBox2() {
		return comboBox2;
	}

	public void setComboBox2(JComboBox<String> comboBox2) {
		this.comboBox2 = comboBox2;
	}

	public void setRadioText(String radioText) {
		this.radioText = radioText;
	}

	public String getRadioText() {
		return radioText;
	}

	public JRadioButton getBt4() {
		return bt4;
	}

	public void setBt4(JRadioButton bt4) {
		this.bt4 = bt4;
	}

	public JRadioButton getBt5() {
		return bt5;
	}

	public void setBt5(JRadioButton bt5) {
		this.bt5 = bt5;
	}

	public JTextField getTf7() {
		return tf7;
	}

	public void setTf7(JTextField tf7) {
		this.tf7 = tf7;
	}

	public JTable getUserTable() {
		return userTable;
	}

	public void setUserTable(JTable userTable) {
		this.userTable = userTable;
	}

	public JPanel getContentPane1() {
		return contentPane1;
	}

	public void setContentPane1(JPanel contentPane1) {
		this.contentPane1 = contentPane1;
	}

	public ButtonGroup getGroup() {
		return group;
	}

	public void setGroup(ButtonGroup group) {
		this.group = group;
	}
	
}
